package com.cgmnx.admin.common.util.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;


@Slf4j
public class DateJsonDeserializer extends JsonDeserializer<Date> {

    private static final String YYYY_MM_DD_HH_MM_SS_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {

        String text = null;

        try {
            text = jsonParser.getText();
            return DateUtils.parseDate(text, YYYY_MM_DD_HH_MM_SS_FORMAT);
        } catch (ParseException e) {
            log.error("解析日期错误,日期:{}", text, e);
        } catch (IOException e) {
            log.error("解析日期错误,日期:{}", text, e);
        }

        return null;
    }
}
